package com.tianhua.javs.core.func.script;

import com.tianhua.javs.client.bean.JavsSegmentBean;
import com.tianhua.javs.client.bean.ScriptVarMappingBean;
import com.tianhua.javs.client.bean.TranslateResultBean;
import com.tianhua.javs.client.func.IScriptFunc;
import com.tianhua.javs.client.func.ITranslatorFunc;
import com.tianhua.javs.core.func.TranslatorContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * Description:
 * date: 2022/2/11
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Component(value = "returnFunc")
public class ReturnFunc  implements IScriptFunc {

    @Autowired
    private TranslatorContainer translatorContainer;


    @Override
    public TranslateResultBean translate(String keyWardStr, Map<String, List<ScriptVarMappingBean>> javsVarMap) {
        //rettrue  返回false
        //retfalse
        //ret:
        //ret(a):返回指定值a
        String tempSegment = keyWardStr;
        String trimSegment = keyWardStr.trim();

        Component component = ReturnFunc.class.getAnnotation(Component.class);
        List<ITranslatorFunc> translatorFuncList = translatorContainer.getByScriptFuncBean(component.value());
        for (ITranslatorFunc translatorFunc : translatorFuncList) {
            JavsSegmentBean javsSegmentBean = translatorFunc.translate(trimSegment);
            if (javsSegmentBean.match()) {
                return TranslateResultBean.getInstance(javsSegmentBean);
            }
        }

        return TranslateResultBean.getInstance(tempSegment);
    }


   /* private String replace(String tempSegment, Map<String, List<ScriptVarMappingBean>> javsVarMap,String keySuffix,String valueSuffix){
        for (Map.Entry<String,List<ScriptVarMappingBean>> entry : javsVarMap.entrySet()){
            for (ScriptVarMappingBean scriptVarMappingBean : entry.getValue()){
                tempSegment = tempSegment.replace(scriptVarMappingBean.getJavsVarCode()+keySuffix,scriptVarMappingBean.getJavaCode()+valueSuffix);
            }
        }

        return tempSegment;
    }*/
}
